package com.systemic.service.impl;

import com.systemic.mapper.*;
import com.systemic.pojo.*;
import com.systemic.service.GroupInfoService;
import com.systemic.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: GroupInfoServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-02-02 10:25
 * @Version:
 **/
@Service
public class GroupInfoServiceImpl implements GroupInfoService {

    @Resource
    private GroupInfoMapper groupInfoMapper;

    @Resource
    private GroupMindfulnessInfoMapper groupMindfulnessInfoMapper;

    @Resource
    private GroupMindfulnessMapper groupMindfulnessMapper;

    @Resource
    private GroupRelationMapper groupRelationMapper;

    @Resource
    private GroupLogMapper groupLogMapper;

    /**
     * 查看团体列表
     * @param pageVo
     * @return
     */
    @Override
    public List<GroupInfo> selectList(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        return groupInfoMapper.selectListPage(offset,pageSize);
    }

    /**
     * 修改团体信息
     *
     * @param groupInfo
     * @return
     */
    @Override
    @Transactional
    public int update(GroupInfo groupInfo) {
        int row = 0;
        groupInfo.setGmtModified(new Date());
        row = groupInfoMapper.update(groupInfo) + row;
        return row;
    }

    /**
     * 注销团体
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int logoff(Integer id) {
        int row = 0;
        GroupInfo groupInfo = new GroupInfo();
        groupInfo.setId(id);
        // TODO:删除正念团体的关联表（正念成员、正念资源、正念打卡记录）
        Integer groupId = groupInfo.getId();   // 正念团体id

        GroupMindfulnessInfo groupMindfulnessInfo = new GroupMindfulnessInfo();
        groupMindfulnessInfo.setGroupId(groupId);
        // 根据团体id查询有哪些正念活动，如果有进行删除
        List<GroupMindfulnessInfo> infoList = groupMindfulnessInfoMapper.select(groupMindfulnessInfo);
        if (!infoList.isEmpty()){
            for (GroupMindfulnessInfo info:infoList) {
                // 删除活动关联的正念
                GroupMindfulness groupMindfulness = new GroupMindfulness();
                groupMindfulness.setGmId(info.getId());
                row = groupMindfulnessMapper.delete(groupMindfulness)+row;
                // 删除活动打卡记录
                GroupLog groupLog = new GroupLog();
                groupLog.setGmId(info.getId());
                groupLogMapper.delete(groupLog);
                row = groupMindfulnessInfoMapper.delete(groupMindfulnessInfo)+row;
            }
        }
        GroupRelation groupRelation = new GroupRelation();
        groupRelation.setGroupId(groupId);
        row = groupRelationMapper.delete(groupRelation)+row;
        row = groupInfoMapper.delete(groupInfo)+row;
        return row;
    }
}
